//不支持就去掉#  nvm use 14
//const EventEmitter=require('events').EventEmitter;

const {EventEmitter}=require('events')

//const Event=require('./05-event.js')
      
class Dog extends EventEmitter{
  constructor(name,energy){
    super();
    this.#name=name;
    this.#energy=energy;
    this.startTimer();                                      
  }
  get name(){ return this.#name;  }
  get energy(){ return this.#energy;  }
  set energy(e){
    this.#energy=e;
    this.startTimer();                                         
  }

  startTimer(){
    if(this.#timer!=null) return;//如果已经启动了后面的操作就不用执行
    this.#timer=setInterval(()=>{
      if(this.#energy>0){
        this.#energy--;
        this.emit('bark');//事件名字，和on响应的事件名要一致                  
      }else{
        clearInterval(this.#timer);
        this.#timer=null;                      
      }
    } ,1000)  

  }
  #name='';
  #energy=0;
  #timer=null;
}


module.exports=Dog;
